package com.yuqian.JavaSe.snio;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

public class SSocketChannel {
	public static void main(String[] args) {
		SocketChannel s = null;

        try {
            System.out.println("[client]start a client socket:");
            s = SocketChannel.open();
            //s.configureBlocking(true);
            System.out.println("[client]try connect...");
            s.connect(new InetSocketAddress("127.0.0.1", 90));
            System.out.println("[client]after connect");

            ByteBuffer src = ByteBuffer.allocate(20);
            BufferedReader re = new BufferedReader(new InputStreamReader(System.in));
            String ss =re.readLine();
            src.put(ss.getBytes("UTF-8"));
            src.flip(); // 为读准备好
            //Thread.sleep(10000);
            s.write(src);
            s.close();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                s.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
	}
}
